package com.petecat.interchan.system.companyempl.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mhuang.common.util.DataUtils;
import com.petecat.interchan.core.exception.BusinessException;
import com.petecat.interchan.core.service.impl.BaseServiceImpl;
import com.petecat.interchan.protocol.Result;
import com.petecat.interchan.protocol.data.Page;
import com.petecat.interchan.protocol.data.PageVO;
import com.petecat.interchan.protocol.sso.company.vo.SysCompanyQryVO;
import com.petecat.interchan.protocol.sso.companyempl.dto.BsSmsAddDTO;
import com.petecat.interchan.protocol.sso.companyempl.dto.BsSmsQryDTO;
import com.petecat.interchan.protocol.sso.companyempl.dto.BsSmsUpdDTO;
import com.petecat.interchan.protocol.sso.companyempl.vo.BsSmsQryVO;
import com.petecat.interchan.protocol.sso.sysuser.SysUserAddDTO;
import com.petecat.interchan.redis.IdWorker;
import com.petecat.interchan.redis.commands.IRedisExtCommands;
import com.petecat.interchan.system.company.entity.SysCompany;
import com.petecat.interchan.system.company.mapper.ICompanyMapper;
import com.petecat.interchan.system.companyempl.entity.BsSms;
import com.petecat.interchan.system.companyempl.mapper.ICompanyEmplMapper;
import com.petecat.interchan.system.companyempl.service.ICompanyEmplService;

@Service
public class CompanyEmplServiceImpl extends BaseServiceImpl<BsSms, String>
									implements ICompanyEmplService{
	
	@Autowired
	private ICompanyEmplMapper companyEmplMapper;
	
	@Autowired
	private IRedisExtCommands redisExtCommands;
	
	private final static Integer ZERO=0;
	
	private final static String SYS_COMPANY_USER_PREKEY="sys_company_user";
	
	@Autowired
	private Environment environment;
	
	private final static String SYUSER_REDIS_USERID_TO_USER_PREKEY="syur_user_uid_";
	/**
	 * 分页查询公司联系人信息
	 */
	@Override
	public PageVO<BsSmsQryVO> queryCompanyEmplByPage(BsSmsQryDTO bsSmsQryDTO) {
		SysUserAddDTO sysUserAddDTO = redisExtCommands.hget(SYUSER_REDIS_USERID_TO_USER_PREKEY, bsSmsQryDTO.getUserId(), SysUserAddDTO.class);
		if(null == sysUserAddDTO) {
			throw new BusinessException(Result.SYS_RESULT_FAILD,
					environment.getProperty("user_not_exites"));
		}
		String user = "0".equals(sysUserAddDTO.getIsMaster()) ? sysUserAddDTO.getUserid():null;
		bsSmsQryDTO.setUserId(user);
		BsSms bsSms = DataUtils.copyTo(bsSmsQryDTO, BsSms.class);
		if(null != bsSmsQryDTO.getEmplIds()) {
			String[] emplIds = bsSmsQryDTO.getEmplIds().split(",");
			bsSms.setEmplIds(emplIds);
		}
		int count = this.companyEmplMapper.count(bsSms);
		PageVO<BsSmsQryVO> pageVo = new PageVO<>();
		pageVo.setTotalSize(count);
		if(count > 0) {
			Page<BsSms> page = new Page<>();
			page.setRows(bsSmsQryDTO.getRows());
			page.setStart(bsSmsQryDTO.getStart());
			page.setRecord(bsSms);
			List<BsSms> companyList = this.companyEmplMapper.page(page);
			pageVo.setResult(DataUtils.copyTo(companyList, BsSmsQryVO.class));
		}
		return pageVo;
	}
	
	/**
	 * 新增公司联系人基本信息
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void saveCompanyEmpl(BsSmsAddDTO bsSmsAddDTO, String userId) {
		BsSms bsSms = DataUtils.copyTo(bsSmsAddDTO, BsSms.class);
		bsSms.setCreateTime(new Date());
		bsSms.setOperatorTime(new Date());
		bsSms.setId(String.valueOf(new IdWorker().nextId()));
		bsSms.setOperaorUserId(userId);
		bsSms.setUserId(userId);
		bsSms.setStatus("1");
		this.companyEmplMapper.save(bsSms);
		redisExtCommands.hset(ZERO, SYS_COMPANY_USER_PREKEY,bsSms.getId(),bsSms);
	}
	
	/**
	 * 删除公司联系人基本信 修改状态
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void removeCompanyEmplById(String companyEmplId, String state) {
		this.companyEmplMapper.updateCompanyStateById(companyEmplId, state);
		redisExtCommands.hdel(ZERO, SYS_COMPANY_USER_PREKEY, companyEmplId);
	}

	/**
	 * 修改公司联系人基本信息
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void modifyCompanyById(BsSmsUpdDTO bsSmsUpdDTO, String userId) {
		BsSms bsSms = DataUtils.copyTo(bsSmsUpdDTO, BsSms.class);
		bsSms.setOperaorUserId(userId);
		bsSms.setOperatorTime(new Date());
		this.update(bsSms);
		redisExtCommands.hset(ZERO, SYS_COMPANY_USER_PREKEY,bsSms.getId(),bsSms);
	}
	
	/**
	 * 查询所有人员
	 */
	@Override
	public List<BsSms> queryAllCompanyEmpl() {
		List<BsSms> list = this.companyEmplMapper.queryAllCompanyEmpl();
		if(!list.isEmpty()) {
			return list;
		}
		return null;
	}
	
	/**
	 * 通过UserId获取用户list
	 */
	@Override
	public BsSmsQryVO queryCompanyEmplListByUserId(BsSmsQryDTO bsSmsQryDTO) {
		if(null != bsSmsQryDTO.getEmplId()) {
			BsSmsQryVO vo = this.companyEmplMapper.queryCompanyEmplListByUserId(bsSmsQryDTO.getEmplId());
			vo.setSmsRule(bsSmsQryDTO.getSms());
			vo.setEmailRule(bsSmsQryDTO.getEmail());
			vo.setEmplAttr(bsSmsQryDTO.getEmplAttr());
			return vo;
		}
		return null;
	}
	
	
}
